import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/services/CartServices.dart';
import 'package:sp_util/sp_util.dart';

class CheckOutService{

  //计算总价
  static getAllPrice(List checkOutListData){
    double tempAllPrice = 0.0;
    for(int i = 0;i<checkOutListData.length;i++){
      if(checkOutListData[i][CartServices.checked] ==  true){
        if(checkOutListData[i][CartServices.price] is String){
          if(!(checkOutListData[i][CartServices.price].toString().contains("."))){
            int price = int.parse(checkOutListData[i][CartServices.price]);
            int count = checkOutListData[i][CartServices.count];
            tempAllPrice += price * count * 1.0;
          }else{
            double price = checkOutListData[i][CartServices.price]*1.0;
            int count = checkOutListData[i][CartServices.count];
            tempAllPrice += price * count;
          }

        }else{
          double price = checkOutListData[i][CartServices.price]*1.0;
          int count = checkOutListData[i][CartServices.count];
          tempAllPrice += price * count;
        }

      }
    }
    print('allprice:$tempAllPrice');
    return tempAllPrice;
  }

  static removeUnSelectedCartItem() {

    List _cartList = [];
    List _tempList = [];
    //获取购物车数据
    try{
      _cartList = SpUtil.getObjectList(Constants.CARTLIST);
    }catch(e){
      _cartList = [];
    }

    for(int i = 0;i<_cartList.length;i++){
      if(_cartList[i][CartServices.checked] == false){
        _tempList.add(_cartList[i]);
      }
    }

    SpUtil.putObjectList(Constants.CARTLIST, _tempList);
  }

}